{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 不会发生异常\n",
    "\n",
    "### 1. `run_until_complete()` 直接运行函数\n",
    "\n",
    "```py\n",
    "import asyncio\n",
    "\n",
    "\n",
    "async def comain():\n",
    "    raise SystemExit(\"SystemExit\")\n",
    "\n",
    "\n",
    "def main():\n",
    "    loop = asyncio.get_event_loop()\n",
    "    try:\n",
    "        loop.run_until_complete(comain())\n",
    "    except BaseException as e:\n",
    "        print(\"caught {}\".format(e))\n",
    "    finally:\n",
    "        loop.close()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### task 捕获异常后 cancel()\n",
    "\n",
    "```py\n",
    "import asyncio\n",
    "\n",
    "\n",
    "async def comain():\n",
    "    raise SystemExit(\"SystemExit\")\n",
    "\n",
    "\n",
    "def main():\n",
    "    loop = asyncio.get_event_loop()\n",
    "    task = loop.create_task(comain())\n",
    "    try:\n",
    "        loop.run_until_complete(task)\n",
    "    except BaseException as e:\n",
    "        print(\"caught {}\".format(e))\n",
    "        task.cancel()\n",
    "    finally:\n",
    "        loop.close()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用asyncio.wait()去执行\n",
    "\n",
    "```py\n",
    "asyncio.wait\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Task 或者 Future 在**运行中发生错误**，**并且在其内部没有被捕获**，必须在使其执行的地方进行 `cancle()`取消，否则会在`Loop`中抛出异常未回收的错误。但是不影响`Loop`中其他代码的执行。\n",
    "\n",
    "有如下场景：\n",
    "\n",
    "```py\n",
    "import asyncio\n",
    "\n",
    "\n",
    "async def comain():\n",
    "    for i in range(-2, 2):\n",
    "        print(1 / i)\n",
    "\n",
    "\n",
    "async def task():\n",
    "    t1 = loop.create_task(comain())\n",
    "    t2 = loop.create_task(comain())\n",
    "    t3 = loop.create_task(comain())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    loop = asyncio.get_event_loop()\n",
    "    try:\n",
    "        loop.run_until_complete(task())\n",
    "    except BaseException as e:\n",
    "        print(\"caught: {}\".format(e))\n",
    "    finally:\n",
    "        loop.close()\n",
    "    print('其他')\n",
    "```\n",
    "\n",
    "\n",
    "即使捕获到了异常，依然会抛出错误信息，`Task exception was never retrieved' 根据提示，我们应该另需处理 Task 中的异常，可以直接将异常的Task或者 Future取消执行即可，cancel()\n",
    "\n",
    "```py\n",
    "import asyncio\n",
    "\n",
    "\n",
    "async def comain():\n",
    "    raise SystemExit(\"SystemExit\")\n",
    "\n",
    "\n",
    "def main():\n",
    "    loop = asyncio.get_event_loop()\n",
    "    task = loop.create_task(comain())\n",
    "    try:\n",
    "        loop.run_until_complete(task)\n",
    "    except BaseException as e:\n",
    "        print(\"caught {}\".format(e))\n",
    "        # 取消执行\n",
    "        task.cancel()\n",
    "    finally:\n",
    "        loop.close()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
